﻿@{
    ViewBag.Title = "编辑任务·自由飞·任务管理系统";
    Layout = "~/Views/Shared/_Layout_Dual_Columns_Big.cshtml";
}
@using FFLTask.GLB.Global.Enum
@model FFLTask.SRV.ViewModel.Task.EditModel
@using Global.Core.ExtensionMethod
<div class="title">
    <span class="h3">编辑任务</span>
    <a href="/Task/New?parentId=@Model.Id" class="owner_opt" id="add_child_task">
        添加子任务
    </a>
</div>
<style>
    #responsible {
        float: right;
        border-bottom: 1px dashed #4c9ed9;
        margin: auto 58px 18px auto;
        padding: 0 0 6px 32px;
        width: 458px;
        font-size: 14px;
    }

        #responsible span {
            margin-left: 6px;
        }

    #node_type {
        float: left;
        width: 60px;
        position: relative;
        left: 30px;
    }
</style>
<div id="node_type">
    @Html.Partial("_NodeType", Model._NodeType, new ViewDataDictionary { { "size", "big" } })
    @Html.ActionLink("关系", "Relation", new { taskId = Model.Id }, new { target = "_blank", style = "font-size: 12px;" })
</div>
<div id="responsible">
    <span style="font-size:16px; font-weight: bold; margin-left: 0;">责任人：</span>
    <span>发布：</span>@Html.Partial("_UserLink", Model.Publisher)
    @if (Model.Owner != null)
    {
        <span>承接：</span>@Html.Partial("_UserLink", Model.Owner)
    }
    <span>验收：</span>@Html.Partial("_UserLink", Model.Accepter)
</div>
@using (Html.BeginForm("Edit", "Task", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.Partial("EditTaskStatus/_Publish", Model)
    @Html.Partial("EditTaskStatus/_AssignOwn", Model)
    @Html.Partial("EditTaskStatus/_RemoveResume", Model)
    @Html.Partial("EditTaskStatus/_InProcess", Model)
    @Html.Partial("EditTaskStatus/_Accept", Model)
    @Html.EditorFor(m => m, "_EditComment")
    <input type="hidden" name="UploadForPublish" id="UploadForPublish" />
    <input type="hidden" name="UploadForComment" id="UploadForComment" />
    <div id="dual_columns_big_input_submit">
        <input type="submit" value="提交" id="dual_columns_big_input_submit_input" />
        <img alt="处理中..." src="/Resource/Image/waiting.gif" id="submitting" style="margin-top: 20px;" />
        @Html.HiddenFor(m => m.CanAutoCompleteParent)
        @Html.HiddenFor(m => m.AutoCompleteParent)
        @Html.HiddenFor(m => m.CanAutoAccepterParent)
        @Html.HiddenFor(m => m.AutoAcceptParent)
        转到：
        <label>
            @Html.RadioButtonFor(m => m.Redirect, RedirectPage.Current) 当前
        </label>
        @if (Model.PreviousTaskId.HasValue)
        {
            @Html.HiddenFor(m => m.PreviousTaskId)
            @Html.RadioButtonFor(m => m.Redirect, RedirectPage.Previous)
            <a href="/Task/Edit/@Model.PreviousTaskId" target="_blank">上个</a>
        }
        @if (Model.NextTaskId.HasValue)
        {
            @Html.HiddenFor(m => m.NextTaskId)
            @Html.RadioButtonFor(m => m.Redirect, RedirectPage.Next)
            <a href="/Task/Edit/@Model.NextTaskId" target="_blank">下个</a>
        }
        @{string parentUrl = Model.Publish.Parent != null ?
              "/Task/Edit/" + Model.Publish.Parent.Id :
              string.Empty;}
        @if (Model.Publish.Parent != null)
        {
            @Html.HiddenFor(m => m.Publish.Parent.Id)
            @Html.RadioButtonFor(m => m.Redirect, RedirectPage.Parent)

            <a href="@parentUrl" target="_blank">父</a>
        }
        任务|<label>
            @Html.RadioButtonFor(m => m.Redirect, RedirectPage.Close) 关闭
        </label>
    </div>
    <div id="ParentCanAutoCompleteReminider" title="父任务自动完成" style="display: none">
        <p>
            该任务的<a href="@parentUrl" target="_blank">父任务</a>为虚任务，且其他兄弟任务都已完成，是否自动完成其父任务
        </p>
    </div>
    <div id="ParentCanAutoAcceptReminider" title="父任务自动验收" style="display: none">
        <p>
            该任务的<a href="@parentUrl" target="_blank">父任务</a>为虚任务，且其他兄弟任务都已验收合格，是否自动验收其父任务
        </p>
    </div>
}
@section RightTitle{
    <h2>
        任务历史
        <span style="font: 12px normal; margin-left: 24px" class="owner_opt">当前状态：@Model.CurrentStatus.Name</span>
    </h2>
}
@section Introduction{
    @Html.Action("History", Model.Id)
}
@section JavaScript{
    <script src="@Url.Content("~/Resource/kindeditor-v4.1/kindeditor-min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Resource/kindeditor-v4.1/lang/zh_CN.js")" type="text/javascript"></script>
    <script type="text/javascript">
        var editorComment;
        var isSubmit = false;
        window.onbeforeunload = function () {
            editorComment.sync();
            if (!isSubmit && $("#@Html.IdFor(m => m.Comment)").val() != "") {
                return "页面内容还没有被保存，确认要离开？";
            }
        }

        function normalSubmit(trigger, event){
            editorComment.sync();
            isSubmit = true;
            if ($("[name='@Html.NameFor(m => m.Accept.AcceptOrNot)'][value='False']").is(":checked") &&
                $("#@Html.IdFor(m => m.Comment)").val() == "") {
                $("[data-valmsg-for='Comment']").text("* 拒绝验收时必须说明原因。").addClass("field-validation-error").show();
                event.preventDefault();
                return false;
            };
            $("form").submit();
            preventDuplicatedSubmit(trigger);
        }

        function canAutoComplete(){
            return $("[type='checkbox'][value='@TaskProcess.InProcess']").is(":checked") &&
                   $("#@Html.IdFor(m => m.CanAutoCompleteParent)").val()=="True" &&
                   $("#@Html.IdFor(m => m.Progress.SelectedQualifiedStatus.Stage) ").val() == @((int)Status.Complete);
        }

        function canAutoAccept(){
            return $("[type='checkbox'][value='@TaskProcess.Accept']").is(":checked") &&
                   $("#@Html.IdFor(m => m.CanAutoAccepterParent)").val()=="True" &&
                   $("#@Html.IdFor(m => m.Accept.AcceptOrNot):checked").val() =="True";
        }

        $(document).ready(function () {
            $("#submitting").hide();

            KindEditor.ready(function (K) {
                editorComment = K.create('textarea[name="@Html.NameFor(m=>m.Comment)"]', {
                    allowFileManager: false,
                    @Html.Partial("KindEditorItems/_Simple")
                });
            });

            $("#removeOrResume").click(function () {
                if ($(this).is(":checked")) {
                    $("#dual_columns_big_input_submit_input").attr("disabled", false);
                }
            });

            $("#@Html.IdFor(m => m.Publish.CurrentProject.TailSelectedProject.Id)").attr("disabled", false);

            preventEnterSubmit();

            $("#dual_columns_big_input_submit_input").click(function (event) {
                $button = $(this);
                if (canAutoComplete()) {
                    event.preventDefault();
                    dialogConfirm($("#ParentCanAutoCompleteReminider")).then(function(answer){
                        if (answer=="true") {
                            $("#@Html.IdFor(m => m.AutoCompleteParent)").val("True");
                        }else{
                            $("#@Html.IdFor(m => m.AutoCompleteParent)").val("False");
                        }
                        normalSubmit($button, event);
                    });
                }else if(canAutoAccept()){
                    event.preventDefault();
                    dialogConfirm($("#ParentCanAutoAcceptReminider")).then(function(answer){
                        if (answer=="true") {
                            $("#@Html.IdFor(m => m.AutoAcceptParent)").val("True");
                        }else{
                            $("#@Html.IdFor(m => m.AutoAcceptParent)").val("False");
                        }
                        normalSubmit($button, event);
                    });
                }else{
                    normalSubmit(this, event);
                }
            });

            $(".date_picker").datepicker();
        })
    </script>

    @*file upload*@
    <script type="text/javascript">
        $(document).ready(function () {

            var $initUpload = $(".init_upload");
            var $continueUpload = $(".continue_upload");

            $initUpload.change(uploadChange);
            $continueUpload.hide().click(function (event) {
                event.preventDefault();

                var uploadInput =
                    "<input type='file' name='file' /> " +
                    "<span class='reminder'>（* 页面提交时上传文件）</span>" +
                    "<a href='#' class='delete_upload_file'>删除</a>" +
                    "<br />";
                $(this).parent().append(uploadInput)
                    .find("input[name=file]:last").change(uploadChange);

                $("a.delete_upload_file").click(function (event) {
                    event.preventDefault();
                    $(this).parent().next().remove();
                    $(this).parent().remove();
                    uploadChange();
                });

                $(this).hide();
            });

            function uploadChange() {

                var $parent = $(this).parent();
                var $continueUpload = $parent.children("a.continue_upload");

                //assume that the continue link should be shown
                $continueUpload.show();

                //but check only show continue link when all the file input has value
                $parent.children("input[name=file]").each(function () {
                    if ($(this).val() == "") {
                        $continueUpload.hide();
                    }
                });

                //mark the uploaded files belongs to Publish or Comment
                var $uploaded;
                var belong = $parent.attr("data-belong");
                if (belong=="Publish") {
                    $uploaded = $("#UploadForPublish");
                }else if (belong=="Comment") {
                    $uploaded = $("#UploadForComment");
                }
                var before = $uploaded.val();
                $uploaded.val(before + "|" + $(this).val().split("\\").pop());
            }
        });
    </script>

    @*checkbox in the subtitle*@
    <script type="text/javascript">
        $(document).ready(function () {
            var $allInputs = $("table[id^='edit_column_']").find("input,select,#previous_project,#next_project");
            $allInputs.attr("disabled", true);
            $("[edit]").click(function () {
                var $myControll = $(this).parents("div.subtitle_container").next();
                var $myInput = $myControll.find("input,select,#previous_project,#next_project");
                if ($(this).is(":checked")) {
                    $myInput.attr("disabled", false);
                    if ($myControll.find("textarea[name$='Body']").length>0 &&
                        typeof editorBody != "undefined") {
                        editorBody.readonly(false);
                    }
                } else {
                    $myInput.attr("disabled", true);
                    if (typeof editorBody != "undefined") {
                        editorBody.readonly(true);
                    }
                }

                if ($(this).val() == '@(TaskProcess.Remove)') {
                    if ($(this).is(":checked")) {
                        $("[name='@(TaskProcess.InProcess)']").attr("disabled", "disabled");
                    }
                    else {
                        $("[name='@(TaskProcess.InProcess)']").removeAttr("disabled");
                    }
                }
            });
        })
    </script>

    <script>
        function refreshProject(event) {
            var projectId = get_current_project_id($(event));
            canPublish(projectId);
            refreshOwner(projectId, "#@Html.IdFor(x => x.Assign.SelectedOwnerId)");
            refreshDifficulties(projectId, "#@Html.IdFor(x => x.Publish.SelectedDifficulty)");
            refreshPriority(projectId, "#@Html.IdFor(x => x.Publish.SelectedPriority)");
            refreshAccepter(projectId, "#@Html.IdFor(x => x.Publish.SelectedAccepterId)");
        }
    </script>
}
